from matplotlib import pyplot as plt
from skimage import data,color

#灰度值到彩色变换,gray=灰色图像
def gray_rgb(gray):
    import numpy as np
    colorimg=np.zeros((gray.shape[0],gray.shape[1],3),dtype='uint8')
    L=255
    for i in range(gray.shape[0]):
        for j in range(gray.shape[1]):
            #Get R 红色变换
            if gray[i,j]<L/2:
                r=0
            elif gray[i,j]>3*L/4:
                r=L
            else:
                r=4*gray[i,j]-2*L
            #Get G  绿色变换
            if gray[i,j]<L/4:
                g=4*gray[i,j]
            elif gray[i,j]>3*L/4:
                g=4*L-4*gray[i,j]
            else:
                g=L
            #Get B 蓝色变换
            if gray[i,j]<L/4:
                b=L
            elif gray[i,j]>L/2:
                b=0
            else:
                b=2*L-4*gray[i,j]
            colorimg[i,j,:]=(r,g,b)
    return colorimg

#彩色到灰度变换
def rgb2gray(label2rgb):
    grayimg=color.rgb2gray(img)#将彩色图像转换为灰度图像
    rows,cols=grayimg.shape
    labels=np.zeros([rows,cols])
    #可以改变灰度值得范围来改变强度分层后的图像
    for i in range(rows):
        for j in range(cols):
            if(grayimg[i,j]<0.015):
                labels[i,j]=0
            elif(grayimg[i,j]<0.118):
                labels[i,j]=1
            else:
                labels[i,j]=2
    psdimg=color.label2rgb(labels)#不同灰度区间采用不同的颜色
    plt.figure()
    plt.axis('off')
    plt.imshow(psdimg)#显示强度分层图像
    return psdimg











